package org.advanced.io;

import java.io.*;

/**
 * @author John.zhang
 * @version Id: Objects, v 1.0 2020/12/14 15:00 ZhiYong Exp $
 */
public class Objects {

    public static void main(String[] args) {
        User user = new User("John", 18);
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("C:/data/io/User"));) {
            objectOutputStream.writeObject(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try (ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("C:/data/io/User"))) {
            User userInfo = (User) objectInputStream.readObject();
            System.out.println(userInfo);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class User implements Serializable {
        private String name;

        private int age;

        public User() {
        }

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

}
